package com.ruoyi.web.controller.resturant;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResShop;
import com.ruoyi.resturant.service.IResShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

    @Anonymous
    @RestController
    @RequestMapping("/api/resturant/shop")
    @Api(tags="店铺管理")
    public class ResShopController extends BaseController {
        @Autowired
        private IResShopService resShopService;
        @Autowired
        private RedisCache redisCache;
        @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "店铺名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "adress", value = "店铺地址", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "contact", value = "联系电话", dataType = "String", dataTypeClass = String.class),
                @ApiImplicitParam(name = "cover", value = "封面", dataType = "String", dataTypeClass = String.class),
                @ApiImplicitParam(name = "detail", value = "店铺详情", dataType = "String", dataTypeClass = String.class),
                @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "String", dataTypeClass = Date.class)
        })
        /**
         * 查询店铺列表
         */
        @ApiOperation("查询店铺列表")
        @ApiImplicitParam(name = "name", value = "店铺名称",
                dataType = "string", dataTypeClass = String.class)
        /**
         获取所有商户列表
        * */

        @GetMapping("/list")
        public AjaxResult list(@RequestParam(value = "name", required = false) String shopName) {
            List<ResShop> list = resShopService.selectResShopByParam(shopName);
            return AjaxResult.success(list);
        }
        /**
         * 获取店铺详细信息
         */

        @ApiOperation(value = "获取店铺详细信息", notes = "TODO：取得某个店铺的详细信息")
        @ApiImplicitParam(name = "id", value = "店铺ID",
                required = true, dataType = "long", paramType = "path",
                dataTypeClass = Long.class)
        @GetMapping(value = "/{id}")
        public AjaxResult getRedisObj(@PathVariable("id") Long id){
            // 1 根据店铺ID从redis取得店铺详情的json字符串
            String str = redisCache.getCacheObject(String.valueOf(id));
            // 2 把店铺详情的json字符串转换为一个model对象ResShop
            ResShop resShop = JSON.parseObject(str, ResShop.class);

            if (resShop == null) {
                // 3 redis不存在这个店铺的场合，从数据库里查询
                resShop = resShopService.selectResShopById(id);
                // 4 数据库里该店铺ID不存在的场合，抛出业务异常BizException
                if(resShop == null) {
                    throw new BizException(404, "该店铺id("+ id + ")不存在");
                }
                String strDB = JSON.toJSONString(resShop);
                redisCache.setCacheObject(String.valueOf(id), strDB,
                        1, TimeUnit.HOURS);
            }

            // 5 返回取得model对象
            return AjaxResult.success(resShop);
        }


        /**
         * 新增店铺
         */
        @Log(title = "店铺管理",businessType = BusinessType.INSERT)

        @ApiOperation("新增店铺")
        @PostMapping
        public AjaxResult add(@RequestBody ResShop resShop) {
            return AjaxResult.success(resShopService.insertResShop(resShop));
        }


        /**
         * 修改店铺
         */
        @Log(title = "店铺管理",businessType = BusinessType.UPDATE)

        @ApiOperation("修改店铺")
        @PutMapping
        public AjaxResult edit(@RequestBody ResShop resShop) {
            //1先根据店铺ID查询数据库
                ResShop resShopDB=  resShopService.selectResShopById(resShop.getId());
            //如果该店铺不存在，就抛出业务异常
            if (resShopDB == null){
                throw  new BizException(404,"该店铺id"+resShop.getId()+"不存在");
            }
            //如果店铺存在，执行更新操作
            return AjaxResult.success(resShopService.updateResShop(resShop));
        }

        /**
         * 删除店铺
         */
        @Log(title = "店铺管理",businessType = BusinessType.DELETE)

        @ApiOperation("删除店铺")
        @ApiImplicitParam(name = "id", value = "店铺ID", required = true, dataType = "long", paramType = "path",
                dataTypeClass = Long.class)
        @DeleteMapping("/{id}")
        public AjaxResult remove(@PathVariable Long id) {
            return AjaxResult.success(resShopService.deleteResShopById(id));
        }
    }


